package service;

import common.*;
import dao.*;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ServerConnectionClient extends Thread {
    private Socket socket;
    private String userid;
    private User user;
    public ServerConnectionClient(Socket socket,String id,User user){
        this.socket=socket;
        userid=id;
        this.user=user;
    }
    public Socket getSocket() {
        return socket;
    }
    @Override
    public void run(){
        FlushCourse.flush();
        while(true){
            System.out.println("服务端正在接收客户端消息...");
            try {
                ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
                //ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                Message ms=(Message) ois.readObject();
                if(ms.getState()== MessageType.new_Class){
                    Course course=(Course) ois.readObject();
                    if(NewClass.newClass(course)){
                        ms.setState(MessageType.new_Success);
                        ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(ms);
                        FlushCourse.flush(userid);//刷新课程列表
                    }else{
                        ms.setState(MessageType.new_Fail);
                        ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(ms);
                    }
                }else if(ms.getState()==MessageType.add_Class){
                    StuMidClass stuMidClass=(StuMidClass) ois.readObject();
                    Course course=GetCourse.getCourse(stuMidClass.getCode());
                    if(course==null){
                        ms.setState(MessageType.add_Fail);
                        ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(ms);
                    }else{
                        if(AddClass.addClass(stuMidClass.getStudentId(),course.getId())){
                            FlushCourse.flush(GetCourse.getCourse(stuMidClass.getCode()).getTeacherId());
                            ms.setState(MessageType.add_Success);
                            ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                            oos.writeObject(ms);
                            FlushCourse.flush(userid);
                        }else{
                            ms.setState(MessageType.add_Fail);
                            ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                            oos.writeObject(ms);
                        }
                    }
                }else if(ms.getState()==MessageType.change_info){
                    User change=(User) ois.readObject();
                    ChangeInfo.infoChange(change);
                }else if(ms.getState()==MessageType.Stu_Add){
                    StuMidClass stuMidClass=(StuMidClass) ois.readObject();
                    if(AddStu.addStu(stuMidClass)){
                        ms.setState(MessageType.Stu_Add_Success);
                        ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(ms);
                        FlushCourse.flush(userid);
                    }else{
                        ms.setState(MessageType.Stu_Add_Fail);
                        ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                        oos.writeObject(ms);
                    }
                }else if(ms.getState()==MessageType.Stu_Dele){
                    StuMidClass stuMidClass=(StuMidClass) ois.readObject();
                    DeleClass.dele(stuMidClass.getStudentId(),stuMidClass.getCourse().getId());
                    FlushCourse.flush(stuMidClass.getCourse().getTeacherId());
                    FlushCourse.flush(stuMidClass.getStudentId());
                }else if(ms.getState()==MessageType.Course_Logout){
                     Course course=(Course) ois.readObject();
                     LogoutCourse.logoutCourse(course);
                }else if(ms.getState()==MessageType.SelectTask_New){
                    Select select=(Select) ois.readObject();
                    NewSelectTask.newSelect(select);
                    FlushCourse.flush(userid);
                }else if(ms.getState()==MessageType.Select_Peop){
                    SelectStu selectStu=(SelectStu) ois.readObject();
                    SelectPeop.selectPeop(selectStu);
                    FlushCourse.flush(userid);
                }else if(ms.getState()==MessageType.Publish_Work){
                    Topic topic=(Topic) ois.readObject();
                    PublishWork.publishWork(topic);
                }else if(ms.getState()==MessageType.Submit_Work){
                    Homework homework=(Homework) ois.readObject();
                    SubmitHW.submitHW(homework);
                }else if(ms.getState()==MessageType.Correct_HW){
                    Homework homework=(Homework) ois.readObject();
                    CorrectHW.correctHW(homework);
                }
            } catch (Exception e) {
                System.out.println("服务端接收客户端消息出错,退出登录");
                ControlSCConnectionThread.remove(userid);
                System.out.println(userid+"下线了");
                FlushCourse.flush();
                e.printStackTrace();
                break;
            }
        }
    }
}
